package com.imooc.sm.controller;


import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.SelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller("selfController")
public class SelfController {

    @Autowired
    private SelfService selfService;

    //  访问登录界面 /toLogin.do    -> login.jsp
    public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/login.jsp").forward(request,response);

    }

    //  登录 /Login.do
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");

        Staff staff = selfService.login(account,password);
        if (staff==null){
            response.sendRedirect("toLogin.do");
        }else {
            HttpSession session = request.getSession();
            session.setAttribute("USER",staff);
            System.out.println("当前登录的用户为："+session.getAttribute("USER"));
            response.sendRedirect("main.do");
        }
    }

    //  退出 /logout.do
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            System.out.println("用户登出方法执行");
            HttpSession session = request.getSession();
            session.setAttribute("USER",null);
            response.sendRedirect("toLogin.do");

    }


    //   访问主界面 /main.do        -> index.jsp
    public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    //   个人信息界面 /self/info.do
    public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../info.jsp").forward(request,response);
    }


    //  访问修改密码界面 /self/toChangPassword.do
    public void toChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../change_password.jsp").forward(request,response);
    }


    //  修改密码 /self/changePassword.do
    public void changePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        HttpSession session = request.getSession();
        Staff staff = (Staff)session.getAttribute("USER");
        if (!staff.getPassword().equals(password)){
            //原始密码错误
            response.sendRedirect("toChangePassword.do");
        }else{
            selfService.changePassword(staff.getId(),password1);

            //前端的父模块跳转，不然会出现嵌套问题
            response.getWriter().print("<script type=\"text/javascript\">parent.location.href=\"../logout.do\"</script>");
            response.sendRedirect("../logout.do");
        }

    }


}
